package com.xxkj.drugtraceability.ui.prescriptiondrugback

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DrugInfoBean
import com.xxkj.drugtraceability.databinding.LayoutPrescriptiondrugcollectionBinding
import com.xxkj.drugtraceability.ext.clickN
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import com.xxkj.drugtraceability.databinding.LayoutDrugScan2Binding

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class PrescriptionDrugBackBottomAdapter(private val actionClick: (index: Int) -> Unit) :
    BaseQuickAdapter<DrugInfoBean, BaseDataBindingHolder<LayoutPrescriptiondrugcollectionBinding>>(R.layout.layout_prescriptiondrugcollection) {

    override fun convert(
        holder: BaseDataBindingHolder<LayoutPrescriptiondrugcollectionBinding>, item: DrugInfoBean
    ) {
        holder.dataBinding?.apply {
            val buffer = StringBuffer()
            buffer.append("<font color='#33333F' size='50px'>")
            buffer.append("<b>" + item.drugName + "    " + "</b>")
            buffer.append("</font>")/*
            buffer.append("<font color='#494953' size='24px'>")
            buffer.append(item.drugSpec?:"")
            buffer.append("</font>")*/
            if (item.trdnFlag == "1") {
                //当前药品是否是拆零标识  1是 0否
                try {
                    buffer.append("<img src='" + R.mipmap.icon_unpack + "'/>")
                    tvContent.text = Html.fromHtml(buffer.toString(), {
                        var drawable = ContextCompat.getDrawable(context, R.mipmap.icon_unpack)
                        drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                        drawable!!
                    }, null)
                } catch (e: Exception) {
                    tvContent.text = Html.fromHtml(buffer.toString())
                }
            } else {
                tvContent.text = Html.fromHtml(buffer.toString())
            }

            tvGg.text = item.drugSpec?.trim() + "   ${item.drugAmount}￥"
            if (item.shelfPosi.isNullOrEmpty()) {
                tvHjh.text = "货架号：无"
            } else {
                tvHjh.text = "货架号：" + item.shelfPosi
            }
            tvRight.text =
                item.drugNum?.toDouble()?.toInt()?.toString() + (item.drugRetailSoec ?: "")
            llAdd.removeAllViews()
            //被选中
            llParent.isSelected = item.isSelected
            llParent.setOnClickListener {
                item.isSelected = !item.isSelected
                if (item.isSelected) {
                    actionClick.invoke(getItemPosition(item))
                } else {
                    actionClick.invoke(-1)
                }
                notifyDataSetChanged()
            }
            if (item.drugTracInfoList == null) {
                item.drugTracInfoList = mutableListOf()
            }
            if (item.drugTracInfoList.isNullOrEmpty()) {
                llBottom.visibility = View.GONE
            } else {
                llBottom.visibility = View.VISIBLE
                item.drugTracInfoList?.forEach { drugTrace ->
                    val viewDrug =
                        LayoutInflater.from(context).inflate(R.layout.layout_drug_scan_2, null)
                    val bind = DataBindingUtil.bind<LayoutDrugScan2Binding>(viewDrug)
                    bind?.apply {
                        data = drugTrace.drugTracCodg
                        llMore.visibility = View.GONE
                        ivDelete.visibility = View.VISIBLE
                        ivDelete.tag = viewDrug
                        ivDelete.clickN {
                            //删除
                            item.drugTracInfoList?.remove(drugTrace)
                            llAdd.removeView(ivDelete.tag as View)
                            if (item.drugTracInfoList.isNullOrEmpty()) {
                                val position = holder.adapterPosition
                                if (position != -1) {
                                    removeAt(position) // 直接调用 adapter 的 removeAt 方法
                                }
                            }
                        }
                        if (item.trdnFlag == "1") {
                            etDrugNum.visibility = View.VISIBLE
                            if (drugTrace.drugTracCodeNum != null) {
                                etDrugNum.setText(drugTrace.drugTracCodeNum.toString())
                            }
                            etDrugNum.doAfterTextChanged {
                                try {
                                    drugTrace.drugTracCodeNum = Integer(it!!.toString())
                                } catch (e: Exception) {
                                }
                            }
                            etDrugNum.setOnFocusChangeListener { _, hasFocus ->
                                if (!hasFocus && etDrugNum.text.isNullOrEmpty()) {
                                    drugTrace.drugTracCodeNum = Integer(1)
                                    etDrugNum.setText("1")
                                }
                            }
//                            etDrugNum.isEnabled = false
                        } else {
                            drugTrace.drugTracCodeNum = Integer(1)
                        }

                        llAdd.addView(viewDrug)
                    }
                }

            }
        }

    }
}